package com.redlion.alt;

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.util.math.MathUtils;
import org.andengine.util.modifier.ease.EaseLinear;

import android.util.Log;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.redlion.Game;
import com.redlion.object.Planet;
import com.redlion.weapon.OnFireListener;

public class Weapon {

	private static final String TAG = "Weapon";
	private static final float SHOT_OFFSET = 20;
	public Sprite sprite;
	public Body body;
	public float centerX;
	public float centerY;
	public IEntityModifier rotation;
	public boolean isLoading = true;
	public float MAX_LOAD_TIME = 1f;
	public float currentLoadTime = 0f;
	protected int FRAME_COLS = 4;
	protected int FRAME_ROWS = 2;
	protected float FRAME_RATE = 1f;
	public OnFireListener shotListener;
	private Planet planet;

	public Weapon(float x, float y, Planet planet) {
		this.planet = planet;
		this.centerX = x;
		this.centerY = y;
		FRAME_COLS = 2;
		FRAME_ROWS = 2;
		FRAME_RATE = 0.1f;
		MAX_LOAD_TIME = FRAME_RATE * FRAME_COLS * FRAME_ROWS;
		// this.animation(Gdx.files.internal(TextureUtil.TEXTUR_LASER));
	}

	public void createSprite(ITextureRegion planetTextureRegion) {
		sprite = new Sprite(centerX - (planetTextureRegion.getWidth() / 2),
				centerY - (planetTextureRegion.getHeight() / 2),
				planetTextureRegion,
				Game.gameActivity.getVertexBufferObjectManager());
	}

	public void alignToPlanet(Planet p, float offset) {
		sprite.setPosition(p.centerX + offset - (sprite.getWidth() / 2),
				p.centerY - (sprite.getHeight() / 2));
		sprite.setRotationCenterX(-offset + (sprite.getWidth() / 2));
	}

	public void createBody(PhysicsWorld physicsWorld, FixtureDef fixtureDef) {
		body = PhysicsFactory.createCircleBody(physicsWorld, sprite,
				BodyType.DynamicBody, fixtureDef);
	}

	public void addShotListener(OnFireListener listener) {
		this.shotListener = listener;
	}

	public void enableRotation(float duration) {
		rotation = new RotationModifier(duration, 0, 360,
				EaseLinear.getInstance()) {
			@Override
			protected void onSetValue(final IEntity pEntity,
					final float pPercentageDone, final float pRotation) {
				if (pPercentageDone == 1) {
					this.reset();
				}
				super.onSetValue(pEntity, pPercentageDone, pRotation);
			}
		};
		sprite.registerEntityModifier(rotation);
		// body.setAngularVelocity(omega)
	}

	public void enableRotation(IEntityModifier rotation) {
		sprite.registerEntityModifier(rotation);
	}

	public void update(float delta) {
		currentLoadTime += delta;
		if (currentLoadTime < MAX_LOAD_TIME)
			return;
		// Timer.start("AbstractWeapon", "updateAndFire");
		fire();
		// Timer.end(false);
		currentLoadTime = 0f;
	}

	void fire() {
//		if (shotListener != null) {
//			float degAngle = MathUtils.degToRad(sprite.getRotation());
//			Vector2 dir = new Vector2((float) Math.cos(degAngle),
//					(float) Math.sin(degAngle)).nor();
//			Vector2 start = planet.center.cpy().add(
//					dir.cpy().mul(planet.raduis+SHOT_OFFSET));
//			Bullet b = new Bullet(start, dir);
//			// b.dir(sprite.getRotation());
//			shotListener.fire(b);
//		}
	}
}
